package com.fanjun.learning.multithread.Two_Phase_Termination;

/**
 * Created by fanjun on 16/10/14.
 */
public class CountThread extends Thread{

    private long counter = 0;
    private volatile boolean shutdownRequest = false;
    @Override
    public void run(){
        try {
            while (!shutdownRequest){
                doWork();
            }
        }catch (InterruptedException ex){
            interrupt();
            System.out.println("是否被中断:"+isInterrupted());
        }finally {
            doShutdown();
            System.out.println("线程是否还活着:" + isAlive());
        }
    }

    private void doShutdown() {
        System.out.println("doShutdown : counter =" + counter);
    }

    private void doWork() throws InterruptedException{
        counter++;
        System.out.println("doWork : counter = " + counter);
        Thread.sleep(500);
    }

    // 终止请求
    public void shutdownRequest() {
        shutdownRequest = true;
        interrupt();

    }

    public boolean isShutdownRequest(){
        return shutdownRequest;
    }

    
}
